﻿using HpxWMS_Api.BLL;
using HpxWMS_Api.CustomAttribute;
using HpxWMS_Api.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace HpxWMS_Api.Controllers
{    /// <summary>
     /// 用户管理接口
     /// </summary>
    [ApiAuthorize]
    public class UserController : ApiController
    {
        private AccountManager userManager = new AccountManager();


        /// <summary>
        /// 获取单个用户信息
        /// </summary>
        /// <param name="id">用户id</param>
        /// <returns></returns>
        //public R<UserDTO> Get(int id)
        //{
        //    var user = userManager.FindById(id);
            
        //    if (user == null)
        //    {
        //        return new R<UserDTO> { Code = "999", Message = "获取失败", Data = user };
        //    }

        //    return new R<UserDTO> { Code = "000", Message = "获取成功", Data = user };
        //}

        /// <summary>
        /// 获取多个用户信息
        /// </summary>
        /// <returns></returns>
        [Route("api/Users")]
        public R<UserPageVO> Getlist([FromUri] Uq uq)
        {
            if(uq == null)
            {
                uq = new Uq();
            }
            List<UserDTO> list = userManager.FindPage(uq);
            UserPageVO userPage = new UserPageVO();
            var total = userManager.Count(uq.SearchText);
            userPage.List = list;
            userPage.Total = total;
            userPage.pageSize = uq.PageSize;
            userPage.PageIndex = uq.PageIndex;

            return new R<UserPageVO> { Code = "000", Message = "操作成功",Data=userPage };
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        [CustomExceptionFilter]
        public R<string> Post([FromBody] Ui ui)
        {
            if (!ModelState.IsValid)
            {
                throw new ClientException(HttpStatusCode.BadRequest);
            }
            if (userManager.Insert(ui))
            {
                return new R<string> { Code = "000", Message = "添加成功" };
            }
            else
            {
                return new R<string> { Code = "999", Message = "添加失败" };
            }

        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <returns></returns>
        public R<string> Put([FromBody] Ue ue)
        {
            if (!ModelState.IsValid)
            {
                throw new ClientException(HttpStatusCode.BadRequest);
            }
            if (userManager.Update(ue))
            {
                return new R<string> { Code = "000", Message = "更新成功" };
            }
            else
            {
                return new R<string> { Code = "999", Message = "更新失败" };
            }
        }
        
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        [Route("api/ChangePwd")]
        public R<string> Put([FromBody] Cpwd cpwd)
        {
            if (!ModelState.IsValid)
            {
                throw new ClientException(HttpStatusCode.BadRequest);
            }
            if (userManager.ChangePwd(cpwd))
            {
                return new R<string> { Code = "000", Message = "更新成功" };
            }
            else
            {
                return new R<string> { Code = "999", Message = "更新失败" };
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <returns></returns>
        public R<string> Delete(int id)
        {
            if (userManager.Delete(id))
            {
                return new R<string> { Code = "000", Message = "删除成功" };
            }
            else
            {
                return new R<string> { Code = "999", Message = "删除失败" };
            }
        }

    }
}
